package com.game.frame;

import com.game.init.Main;
import com.game.utils.DataBaseUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @作者: liwang
 * @时间: 2024/12/17
 * 登录按钮
 * register_username  //用户名输入框
 * login_password //密码输入框
 * registerButton  //注册按钮
 */
public class RegisterFrame extends MyFrame {
    private JPanel contentPane;
    private JTextField register_username;//用户名输入框
    private JTextField register_password;//密码输入框
    private ExecutorService executorService = Executors.newSingleThreadExecutor(); // 声明并初始化线程池

    public RegisterFrame() {
        super();
        this.setTitle("注册页面");
        contentPane = new JPanel();

        // 创建一个按钮 用于回退到首页
        JButton backButton = new JButton("返回");
        backButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Main.setStatus(0);
                dispose();
            }
        });

        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        JLabel lblNewLabel = new JLabel("用户名");

        JLabel label = new JLabel("密码");

        register_username = new JTextField();
        register_username.setColumns(10);

        register_password = new JTextField();
        register_password.setColumns(10);

        JButton registerButton = new JButton("注册");//登录按钮
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 异步执行
                executorService.submit(() -> {
                    if (DataBaseUtil.registerUser(register_username.getText(), register_password.getText())) {
                        SwingUtilities.invokeLater(() -> {
                            JOptionPane.showMessageDialog(RegisterFrame.this, "注册成功", "注册", JOptionPane.INFORMATION_MESSAGE);
                            Main.setStatus(1);
                            dispose();
                        });
                    }else{
                        SwingUtilities.invokeLater(() -> {
                            JOptionPane.showMessageDialog(RegisterFrame.this, "注册出现错误...", "登录失败", JOptionPane.ERROR_MESSAGE);
                        });
                    }
                });
            }
        });
        GroupLayout gl_contentPane = new GroupLayout(contentPane);
        gl_contentPane.setHorizontalGroup(
                gl_contentPane.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addGroup(gl_contentPane.createParallelGroup(GroupLayout.Alignment.LEADING)
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addComponent(backButton) // 添加 backButton 到左上角
                                                .addGap(72)
                                                .addGroup(gl_contentPane.createParallelGroup(GroupLayout.Alignment.LEADING)
                                                        .addComponent(lblNewLabel)
                                                        .addComponent(label))
                                                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(gl_contentPane.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(register_password)
                                                        .addComponent(register_username, GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)))
                                        .addGroup(gl_contentPane.createSequentialGroup()
                                                .addGap(180)
                                                .addComponent(registerButton)))
                                .addContainerGap(95, Short.MAX_VALUE))
        );
        gl_contentPane.setVerticalGroup(
                gl_contentPane.createParallelGroup(GroupLayout.Alignment.LEADING)
                        .addGroup(gl_contentPane.createSequentialGroup()
                                .addComponent(backButton) // 添加 backButton 到左上角
                                .addGap(40)
                                .addGroup(gl_contentPane.createParallelGroup(GroupLayout.Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(register_username, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(31)
                                .addGroup(gl_contentPane.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                        .addComponent(label)
                                        .addComponent(register_password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGap(37)
                                .addComponent(registerButton)
                                .addContainerGap(60, Short.MAX_VALUE))
        );
        contentPane.setLayout(gl_contentPane);
    }

    public void shutdownExecutorService() {
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
        }
    }
}
